package com.book.servlet.auth;

import com.book.service.AdminService;
import com.book.service.impl.AdminServiceImpl;
import com.book.utils.ThymeleafUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.thymeleaf.context.Context;

import java.io.IOException;
import java.util.Objects;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    AdminService adminService;

    @Override
    public void init() throws ServletException {
        adminService = new AdminServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            String username = null;
            String password = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) username = cookie.getValue();
                if (cookie.getName().equals("password")) password = cookie.getValue();
            }
            if (username != null && password != null) {
                //登陆校验
                adminService.auth(username, password, request.getSession());
                response.sendRedirect("index");
                return;
            }
        }

        Context context = new Context();
        if (Objects.nonNull(request.getSession().getAttribute("login-failure"))) {
            context.setVariable("failure", true);
            request.getSession().removeAttribute("login-failure");
        }
        if (Objects.nonNull(request.getSession().getAttribute("user"))) {
            response.sendRedirect("index");
            return;
        }
        ThymeleafUtil.process("login.html", context, response.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("remember");
        if (adminService.auth(username, password, request.getSession())) {
            if (Objects.nonNull(remember)) {
                Cookie cookie_username = new Cookie("username", username);
                cookie_username.setMaxAge(60 * 60 * 24 * 7);
                Cookie cookie_password = new Cookie("password", password);
                cookie_password.setMaxAge(60 * 60 * 24 * 7);
                response.addCookie(cookie_username);
                response.addCookie(cookie_password);
            }
            response.sendRedirect("index");
        } else {
            request.getSession().setAttribute("login-failure", new Object());
            this.doGet(request, response);
        }
    }
}
